//
//  MapMerchantCell.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/6/12.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "MapMerchantCell.h"
#import "TagView.h"
#import "ProgressBtn.h"

@interface MapMerchantCell ()<IGAvPlayerManagerDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UIImageView *merchantImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet TagView *tagView;
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tagViewW;
@property (weak, nonatomic) IBOutlet ProgressBtn *playBtn;

@property (weak, nonatomic) IBOutlet UILabel *eventTagLabel;
@property (weak, nonatomic) IBOutlet UILabel *eventNameLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *eventTagLabelW;

@end

@implementation MapMerchantCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.bgView.layer.cornerRadius = 4.0f;
    
    self.bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.bgView.layer.shadowOpacity = 0.1f;
    self.bgView.layer.shadowOffset = CGSizeMake(0, 1);
    
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.tagView.tagViewWidth = self.tagViewW;
    
    self.eventTagLabel.layer.cornerRadius = 2.0f;
    self.eventTagLabel.layer.borderColor = kThemeRedColor.CGColor;
    self.eventTagLabel.layer.borderWidth = 1.0f;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PlayVideoChangePlayStateWith:) name:kIGAvPlayerManagerState object:nil];
}


- (IBAction)playBtnClicked:(ProgressBtn *)sender {
    
    [IGAvPlayerManager shareManeger].delegate = self;
    
    if (![self.model.playpath isEqualToString:[IGAvPlayerManager shareManeger].currentURL]) {
        [[IGAvPlayerManager shareManeger] playMusic:self.model.playpath withPlayType:PlayTry];
    }else{
        if (![IGAvPlayerManager shareManeger].isPlay) {
            [[IGAvPlayerManager shareManeger] play];
        }else {
            [[IGAvPlayerManager shareManeger] pause];
        }
    }
}


- (void)setModel:(MerchantModel *)model {
    
    _model = model;
    
    [self.merchantImageView setImageURL:[NSURL URLWithString:model.pictureArray.firstObject] placeholderImage:kPlaceholderSqu];
    self.nameLabel.text = model.name;
    self.tagView.tagsArray = model.tagsArray;
    
    self.distanceLabel.text = [IGUtil getDistanceFormatFromCoordinate:CLLocationCoordinate2DMake(model.lat.floatValue, model.lng.floatValue) toCoordinate:[LocationManager sharedManager].currentLocation.coordinate];
    
    
    MerchantEventModel *event = model.promotion.firstObject;
    
    if (!event) {
        self.eventTagLabel.hidden = YES;
        self.eventNameLabel.hidden = YES;
        
        return;
    }
    self.eventTagLabel.hidden = NO;
    self.eventNameLabel.hidden = NO;
    
    CGFloat width = [IGUtil getSizeContent:event.type width:1000 font:Font(12)].width + 15;
    
    self.eventTagLabelW.constant = width;
    
    self.eventNameLabel.text = event.name;
    self.eventTagLabel.text = event.type;
}


#pragma mark -- IGAvPlayerManagerDelegate

- (void)updateProgressWith:(CGFloat)value {
    
    if ([IGAvPlayerManager shareManeger].isPlay) {
        [self.playBtn setProgress:value];
    }
}


- (void)PlayVideoChangePlayStateWith:(NSNotification *)notif
{
    NSInteger state = [notif.userInfo[kIGAvPlayerManagerState] integerValue];
    NSString  * url = notif.userInfo[kCurrentPlayURL];
    
    NSNumber * type = notif.userInfo[kPlayType];
    if (![url isEqualToString:self.model.playpath] || type.integerValue != PlayTry) {
        if (self.playBtn.selected) {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
        }
        
        return;
    }
    switch (state) {
        case AVPlayerPlayStatePreparing:
        case AVPlayerPlayStateBeigin:
        case AVPlayerPlayStatePlaying:
        {
            self.playBtn.selected = YES;
            
            [self.playBtn  startSpinProgressBackgroundLayer];
        }
            break;
        case AVPlayerPlayStatePause:
        case AVPlayerPlayStateNotPlay:
        case AVPlayerPlayStateNotKnow:
        {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
            
        }
            break;
        case AVPlayerPlayStateEnd:
        {
            self.playBtn.selected = NO;
            self.playBtn.progress = 0.f;
        }
            break;
        default:
        {
        }
            break;
    }
    
}
@end
